// component/address/address.js
import {cityJson} from '../../static/js/cityJson.js'
let app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        searchParams: Object,
        isShow: Boolean,
        cdata: Array
    },

    /**
     * 组件的初始数据
     */
    data: {
        proviceArr: [],
        cityArr: [],
        areaArr: [],
        curArr:[0,0,0],
        isCshow:false
    },
    attached:function(){
        let that = this;
        setTimeout(function(){
            that.setData({
                isCshow:true
            })
        })
        if(this.data.cdata.length>0){
            cityJson.forEach((bel, bindex) => {
                if (this.data.cdata[0].d == bel.d) {
                    let bkey = 'curArr[' + 0 + ']'
                    this.setData({
                        [bkey]: bindex
                    })
                    console.log(bel.l)
                    bel.l.forEach((cel, cindex) => {
                        if (this.data.cdata[1].d == cel.d) {
                            let ckey = 'curArr[' + 1 + ']'
                            this.setData({
                                [ckey]: cindex
                            })
                        }
                        cel.l.forEach((del, dindex) => {
                            if (this.data.cdata[2].d == del.d) {
                                let dkey = 'curArr[' + 2 + ']'
                                this.setData({
                                    [dkey]: dindex
                                })
                            }
                        })
                    })
                }

            })
        };
        
        console.log(this.data.curArr)
        this._getProviceData()
    },
    
    /**
     * 组件的方法列表
     */
    methods: {
        //获取省份信息
        _getProviceData() {
            
            let prov = [];
            cityJson.forEach((el, index) => {
                
                prov.push({
                    n:el.n,
                    d:el.d
                })
            })
           
            this.setData({
                proviceArr: prov
            })
            this._getCityData(this.data.proviceArr[this.data.curArr[0]].d)
        },
        //获取市
        _getCityData(provId){
            let cityArr = [];
            let curProvId = provId || this.data.proviceArr[0].d;
            cityJson.forEach((el, index) => {
                if (curProvId == el.d){
                    el.l.forEach((cel,index)=>{
                        
                        cityArr.push({
                            n:cel.n,
                            d:cel.d
                        })
                    })
                }
                
            })
            this.setData({
                cityArr: cityArr
            })
            this._getAreaArr(this.data.proviceArr[this.data.curArr[0]].d, this.data.cityArr[this.data.curArr[1]].d)
        },
        //获取城市信息
        _getAreaArr(provId,cityId){
            console.log(provId, cityId)
            let areaArr = [];
            let curProvId = provId || this.data.proviceArr[0].d;
            let curCityId = cityId || this.data.cityArr[0].d;
            cityJson.forEach((el, index) => {
                if (curProvId == el.d) {
                    el.l.forEach((cel, index) => {
                        if(curCityId == cel.d){
                            cel.l.forEach((del,index)=>{
                                areaArr.push({
                                    n: del.n,
                                    d: del.d
                                })
                            })
                        }
                    })
                }

            })
            
            console.log(this.data.curArr)
            this.setData({
                areaArr: areaArr
            })
            this.setData({
                curArr: this.data.curArr
            })
        },
        //省份发生变化
        _provChange(e){
            this.setData({
                curArr:e.detail.value
            })
            this._getProviceData()
        },
        _hideDialog(e) {
            let dataset = e.currentTarget.dataset;
            if (dataset.type == "1") {
                this.triggerEvent('closePop', {
                    type: 1
                });
            } else {
                let selectedArr = [];
                selectedArr.push(this.data.proviceArr[this.data.curArr[0]])
                selectedArr.push(this.data.cityArr[this.data.curArr[1]])
                selectedArr.push(this.data.areaArr[this.data.curArr[2]])
                this.triggerEvent('closePop', {
                    type: dataset.type,
                    array: selectedArr
                });
            }

        }
    },
    

})